<template>
    <view class="group_list" v-if="state.totalCount > 0">
        <view class="header">
            <view class="title">{{ state.totalCount }}人在拼单，可直接参与</view>
            <view v-if="state.totalCount > 2" class="look_btn" @click="onLook"
                >查看更多<uni-icons type="right" size="12"></uni-icons
            ></view>
        </view>
        <view class="list_container">
            <view class="list" :style="{ transform: `translateY(-${lengthY}px)`, transitionDuration: `${duration}ms` }">
                <view class="item" v-for="item in state.list" :key="item.id + Math.random()">
                    <view class="item_left">
                        <view class="avatar">
                            <template v-for="(info, i) in item.offered" :key="`k${i}`">
                                <image
                                    v-if="i < 3"
                                    :style="{ zIndex: 10 - i }"
                                    :src="info.avatar || getImgOssUrl('/ui-prod-2.7.0/avatar.png')" />
                            </template>
                        </view>
                        <view class="name_box">
                            <view class="nickname">{{ item.offered[0]?.nickname || '匿名' }}</view>
                            <text v-if="item.offeredNum > 1">
                                等<text class="name_num">{{ item.offeredNum }}</text
                                >人
                            </text>
                        </view>
                    </view>
                    <view class="item_right">
                        <view class="info">
                            <view class="desc_num">还差{{ item.surplusNum }}人拼成</view>
                            <view class="countdown"
                                ><countdownVue format="剩余HH小时mm分ss秒" :endTime="getNowTimes(item)"
                            /></view>
                        </view>
                        <view class="to_pingdan" @click="onPindan(item)"> 去拼单 </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script setup>
import { getImgOssUrl } from '@/commons/utils';
import countdownVue from '@/components/countdown';
import request from '@/request';
import { ref, reactive } from 'vue';

const props = defineProps({
    goodsId: {
        type: [String, Number]
    },
    activityId: {
        type: [String, Number]
    }
});
const emit = defineEmits(['opneGroupPopup']);
const onLook = () => {
    emit('opneGroupPopup');
};
const state = reactive({
    list: [],
    totalCount: 0,
    update: false
});
const parmas = reactive({
    goodsId: +props.goodsId,
    activityId: +props.activityId,
    page: 1,
    perPage: 15
});
const getNowTimes = item => {
    let activityEndTime = item.activityEndTime * 1000;
    let groupEndTime = (item.activityStartTime + item.surplusTime) * 1000;
    return groupEndTime < activityEndTime ? groupEndTime : activityEndTime;
};
// 获取拼团列表
const getGroupList = async () => {
    const { data } = await request('getAllGroupListServ', parmas);
    state.list.splice(0, state.list.length, ...data.list);
    state.totalCount = data.totalCount || 0;
    if (state.list.length > 2) {
        state.list.push(...state.list.slice(0, state.list.length % 2 ? 1 : 2));
        onScroll();
    }
};
const lengthY = ref(0);
const duration = ref(600);
let count = 0;
const onScroll = async () => {
    count += 2;
    lengthY.value += 102;
    if (count === state.list.length) {
        duration.value = 0;
        lengthY.value = 0;
        count = 0;
    }
    setTimeout(() => {
        duration.value = 600;
        onScroll();
    }, 3000);
};

const onPindan = item => {
    emit('openGroupSkupop', item);
};

getGroupList();
</script>

<style lang="scss" scoped>
.group_list {
    margin-top: 18rpx;
    background: #fff;
    font-weight: 400;
    color: #333333;
    .header {
        padding: 20rpx 29rpx;
        border-bottom: 2rpx solid #f5f5f5;
        display: flex;
        justify-content: space-between;
        .title {
            font-size: 29rpx;
        }
        .look_btn {
            display: flex;
            font-size: 24rpx;
            color: #999999;
        }
    }
    .list_container {
        max-height: 102px;
        overflow: hidden;
        .list {
            transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
            padding: 0 29rpx;
            transition-duration: 600ms;
            .item {
                // padding: 20rpx 0;
                height: 50px;
                border-bottom: 1px solid #f5f5f5;
                display: flex;
                justify-content: space-between;
                align-items: center;
                &:nth-child(2n) {
                    border-bottom: 1px solid #fff;
                }

                .item_left {
                    margin-right: 20rpx;
                    display: flex;
                    align-items: center;
                    .avatar {
                        max-width: 120rpx;
                        display: flex;
                        flex-wrap: nowrap;
                        overflow: hidden;
                        margin-right: 20rpx;
                        image {
                            width: 60rpx;
                            height: 60rpx;
                            border-radius: 50%;
                            margin-left: -30rpx;
                            &:first-child {
                                margin-left: 0;
                            }
                        }
                    }
                    .name_box {
                        flex: 1;
                        display: flex;
                        font-size: 25rpx;
                        .nickname {
                            max-width: 100rpx;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            word-break: break-all;
                        }
                        .name_num {
                            color: #fe292b;
                        }
                    }
                }
                .item_right {
                    display: flex;
                    align-items: center;
                    .info {
                        margin-right: 18rpx;
                        .desc_num {
                            font-size: 22rpx;
                            text-align: right;
                        }
                        .countdown {
                            font-size: 20rpx;
                            font-weight: 400;
                            color: #999999;
                        }
                    }
                    .to_pingdan {
                        width: 132rpx;
                        height: 60rpx;
                        border-radius: 30rpx;
                        line-height: 60rpx;
                        text-align: center;
                        background: linear-gradient(270deg, #ff5356 0%, #fe292b 100%);
                        color: #fff;
                        font-size: 24rpx;
                    }
                }
            }
        }
    }
}
</style>
